package com.gidgabs.modules.api.controller;

import com.gidgabs.common.utils.ReturnJson;
import com.gidgabs.modules.api.service.TbProductCollectService;
import com.gidgabs.modules.api.service.TbUserService;
import com.gidgabs.modules.entity.TbProductCollectEntity;
import com.gidgabs.modules.entity.TbUser;
import com.gidgabs.utils.SessionDataUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 产品关系表-收藏表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-06-29 17:16:11
 */
@RestController
@RequestMapping("/api/productcollect")
public class TbProductCollectController {

    @Autowired
    private TbProductCollectService productCollectService;

    @Autowired
    private TbUserService userService;


    /**
     * 添加收藏
     *
     * @param productId
     * @return
     */
    @ApiOperation(value = "添加收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "被收藏产品id", dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/add")
    public ReturnJson<String> add(Integer productId) {
        //获取用户信息
        TbUser tbUser = new TbUser();
        tbUser.setUserId(SessionDataUtil.getCurrentUserId());
        TbUser user = userService.queryObject(tbUser);
        if (user == null) {
            return ReturnJson.fail("登录用户不存在！");
        }
        TbProductCollectEntity tbProductCollectEntity = new TbProductCollectEntity();
        tbProductCollectEntity.setProductIdSlave(productId);
        tbProductCollectEntity.setUserIdMaster(user.getUserId());
        productCollectService.save(tbProductCollectEntity);
        return ReturnJson.success("操作成功");
    }

    /**
     * 取消收藏
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "取消收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "被收藏id数据集", dataType = "int[]", paramType = "form"),
            @ApiImplicitParam(name = "token", value = "token", dataType = "string", paramType = "header")
    })
    @PostMapping("/delete")
    public ReturnJson<String> delete(Integer[] ids) {
        productCollectService.deleteBatch(ids);
        return ReturnJson.success("删除成功");
    }

}
